package org.penguin.study.bk.organization.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.penguin.study.bk.base.util.StringUtil;
import org.penguin.study.bk.constant.CommonConstant;
import org.penguin.study.bk.organization.dao.OrganizationMapper;
import org.penguin.study.bk.organization.entity.Organization;
import org.penguin.study.bk.organization.service.OrganizationService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @author chengyinghua
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Override
    @CachePut(value = CommonConstant.SYS_ORGANIZATION_DICT_CACHE, key = "#entity.id")
    public Organization createOrUpdate(Organization entity) {
        if (StringUtil.isEmpty(entity.getId())) {
            super.save(entity);
        } else {
            Organization organization = super.getById(entity.getId());
            if (organization == null) {
                entity.setId(null);
                super.save(entity);
            } else {
                super.updateById(entity);
            }
        }
        return entity;
    }

    @Override
    @CacheEvict(value = CommonConstant.SYS_ORGANIZATION_DICT_CACHE, key = "#entity.id")
    public Organization delete(Organization entity) {
        super.removeById(entity);
        return entity;
    }

    @Override
    @Cacheable(value = CommonConstant.SYS_ORGANIZATION_DICT_CACHE, key = "#id")
    public Organization getById(Serializable id) {
        return super.getById(id);
    }
}
